﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MyLib
{
    /// <summary>
    /// One tick of trading dataDelivery.
    /// </summary>
    [Serializable]
    public struct DataTick
    {
        static DateTime utcZero = new DateTime(1970, 1, 1, 0, 0, 0);
        public int Time { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public float Ask { get; set; }
        public int AskVol { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public float Bid { get; set; }
        public int BidVol { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public float Price { get; set; }
        public float Volume { get; set; }
        public float OpenInt { get; set; }
        public ushort direct { get; set; }

        public override string ToString()
        {
            DateTime dt = utcZero.AddSeconds(Time);
            return dt.ToLongTimeString();
        }
    }

    public struct Order
    {
        public enum BorB { Buy, Sell }

        public BorB Type { get; set; }
        public float Open { get; set; }
        public float Close { get; set; }
        public float Fee { get; set; }
    }

    public struct DailyReport
    {
        public int Win { get; set; }
    }

    public class DataProvider
    {
        string HFTPath;
        string _type;
        string _market;
        DateTime _datatime;
        public DateTime Date
        {
            set { _datatime = value; }
        }

        public string Type
        {
            set
            {
                _market = value.Substring(0, 2);
                _type = value.Substring(2);
            }
        }

        public void SetDate(string datestr)
        {
            if (!DateTime.TryParse(datestr, out _datatime))
            {
                Console.WriteLine("date parse error");
                return;
            }
        }
        
        public DataProvider(string path)
        {
            HFTPath = path;
        }

        public DataProvider()
        {
            HFTPath = @"D:\KingFoxData\zz";
            _type = "SRY09";
            _datatime = new DateTime(2011, 4, 11);
        }

        public DataTick[] readTicks()
        {
            //string filename = @"F:\HistoryData\zz\20110411.QRT";
            string filename;
            string type;
            int i;
            int ai_size, ai_conut, quotes, quote_offset, quote_size;
            int ticks = 0, startblock = 0;
            int opentime = 0;
            filename = Path.Combine(HFTPath, _datatime.ToString("yyyyMMdd") + ".QRT");
            DataTick[] data;

            ai_size = 252;
            quote_size = 36;

            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryReader br = new BinaryReader(fs);
            //br.BaseStream.Seek(4,SeekOrigin.Begin);
            //ai_size = br.ReadInt32();
            br.BaseStream.Seek(12, SeekOrigin.Begin);
            quotes = br.ReadInt32();
            br.BaseStream.Seek(20, SeekOrigin.Begin);
            quote_offset = br.ReadInt32();
            br.BaseStream.Seek(28, SeekOrigin.Begin);
            ai_conut = br.ReadInt32();
            for (i = 0; i < ai_conut; i++)
            {
                br.BaseStream.Seek(32 + ai_size * i, SeekOrigin.Begin);
                type = new string(br.ReadChars(10));
                if (_type.Equals(type.Replace("\0", "")))
                {
                    ticks = br.ReadUInt16();
                    br.BaseStream.Seek(2, SeekOrigin.Current);
                    startblock = br.ReadInt16();
                    break;
                }
            }
            if (ticks != 0)
            {
                data = new DataTick[ticks];
                br.BaseStream.Seek(startblock * quotes * quote_size + quote_offset, SeekOrigin.Begin);
                opentime = br.ReadInt32();
                opentime = opentime - opentime % 86400 + 32400;
                br.BaseStream.Seek(-4, SeekOrigin.Current);
                for (i = 0; i < ticks; i++)
                {
                    data[i].Time = br.ReadInt32();
                    data[i].Price = br.ReadSingle();
                    data[i].Volume = br.ReadSingle();
                    data[i].OpenInt = br.ReadSingle();
                    data[i].BidVol = br.ReadUInt16();
                    br.BaseStream.Seek(4, SeekOrigin.Current);
                    data[i].AskVol = br.ReadUInt16();
                    br.BaseStream.Seek(4, SeekOrigin.Current);
                    data[i].Bid = data[i].Price - br.ReadByte();
                    br.BaseStream.Seek(2, SeekOrigin.Current);
                    data[i].Ask = data[i].Price + br.ReadByte();
                    br.BaseStream.Seek(2, SeekOrigin.Current);
                    data[i].direct = br.ReadUInt16();
                }
            }
            else
            {
                data = new DataTick[0];
            }

            return data;
        }
    }
}
